package cn.founder.org.controller;

import cn.founder.basic.util.AjaxResult;
import cn.founder.basic.util.PageList;
import cn.founder.org.domain.Department;
import cn.founder.org.log.MyLog;
import cn.founder.org.query.DepartmentQuery;
import cn.founder.org.service.IDepartmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/department")
public class DepartmentController {

    @Autowired
    private IDepartmentService departmentService;


    //查询一条数据
    @MyLog("查询单个")
    @GetMapping("/{id}")
    public Department queryOne(@PathVariable("id") Long id){
        return departmentService.queeryById(id);
    }

    //查询所有
    @MyLog("查询所有")
    @GetMapping
    public List<Department> queryAll(){
        return departmentService.queryAll();
    }

    //删除一条数据
    @MyLog("删除单个数据")
    @DeleteMapping("/{id}")
    public AjaxResult delOne(@PathVariable("id") Long id){
        try {
            departmentService.del(id);
            return AjaxResult.myself();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.fail("删除失败");
        }
    }

    //添加或修改
    @MyLog("新增或删除")
    @PutMapping
    public AjaxResult addorUpdata(@RequestBody Department department){
        try {
            if (department.getId()==null){
                departmentService.add(department);
            }else {
                departmentService.update(department);
            }
            return  AjaxResult.myself();
        } catch (Exception e) {
            e.printStackTrace();
            return  AjaxResult.fail("操作失败");
        }
    }

    @MyLog("查询所有加分页")
    @PostMapping
    public PageList<Department> queryPage(@RequestBody DepartmentQuery query){
        return departmentService.queryPage(query);
    }



    //批量删除一条数据
    @MyLog("批量删除")
    @PatchMapping
    public AjaxResult pathchDelete(@RequestBody List<Long> ids){
        try {
            departmentService.pathchDel(ids);
            return AjaxResult.myself();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.fail("删除失败");
        }
    }


    //查询部门树
    @GetMapping("/tree")
    public List<Department> getDeptTree(){
        return departmentService.getDeptTree();
    }
}
